/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-06-26
 * Time: 10:19
 */
//day15 贪心算法练习 力扣135 分发糖果
public class Main {
    //初始化下标数组为1
    public void Init(int[] array) {
        for (int k = 0; k < array.length; k++) {
            array[k] = 1;
        }
    }
    public int candy(int[] ratings) {
        int length = ratings.length;
        int sum = 0;
        int i = 1;
        int j = length-2;
        int[] left = new int[length];
        Init(left);
        //满足左相邻
        for (; i <length ; i++) {
            //判断ratings[i]与ratings[i-1],如果大于则left[i] = left[i-1] + 1,小于则continue
            if(ratings[i] > ratings[i-1]) {
                left[i] = left[i-1] + 1;
            } else {
                continue;
            }
        }
        int[] right = new int[length];
        Init(right);
        //满足右相邻
        for (; j >= 0 ; j--) {
            //判断ratings[i]与ratings[i+1],如果大于则right[i] = right[i+1] + 1,小于则continue
            if(ratings[j] > ratings[j+1]) {
                right[j] = right[j+1] + 1;
            } else {
                continue;
            }
        }
        for (int l = 0; l < length; l++) {
            sum += Math.max(left[l],right[l]);
        }
        return sum;
    }
    public static void main(String[] args) {
        int[] array = {1,0,2};
        int[] array1 = {1,2,2};
        int[] array2 = {1,2,0,3,1};
        int[] array3 = {2,2,2};
        int[] array4 = {1,2,87,87,87,2,1};
        Main main = new Main();
        System.out.println(main.candy(array));
        System.out.println(main.candy(array1));
        System.out.println(main.candy(array2));
        System.out.println(main.candy(array3));
        System.out.println(main.candy(array4));
    }
}
